package com.yishusmt.mobsmt.controller;

import com.yishusmt.mobsmt.check.DataCheck;
import com.yishusmt.mobsmt.dto.base.Result;
import com.yishusmt.mobsmt.dto.request.AddressReq;
import com.yishusmt.mobsmt.entity.ReceivingAddressEntity;
import com.yishusmt.mobsmt.mapper.ReceivingAddressEntityMapper;
import com.yishusmt.mobsmt.service.AddressService;
import com.yishusmt.mobsmt.utils.JacksonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户地址控制
 *
 * @author zhangyi
 * @date 2018/8/11 14:37
 */
@RestController
@RequestMapping("/address")
public class AddressController {
    @Resource
    private AddressService addressService;

    /**
     * 添加地址
     *
     * @param addressReq
     * @return
     */
    @PostMapping("/insert")
    @ApiOperation(value = "添加地址",httpMethod = "POST")
    public Result add(@RequestBody AddressReq addressReq){
        DataCheck.isNull(addressReq);
        return addressService.add(addressReq);
    }

    /**
     * 删除地址
     *
     * @param addressId
     * @return
     */
    @DeleteMapping("/del/{addressId}")
    public Result del(@PathVariable(value = "addressId") Long addressId){
        return addressService.del(addressId);
    }

    /**
     * 修改地址
     *
     * @param receivingAddressEntity
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "用户修改地址",httpMethod = "POST")
    public Result update(@RequestBody ReceivingAddressEntity receivingAddressEntity){
        DataCheck.isNull(receivingAddressEntity);
        return addressService.update(receivingAddressEntity);
    }
    /**
     * 查询所有地址
     *
     * @param phone
     * @return
     */
    @GetMapping("/selectAll/{phone}")
    @ApiOperation(value = "查询所有地址",httpMethod = "GET")
    public Result selectAllAddress(@PathVariable(value = "phone") String phone){
        DataCheck.isNull(phone);
        System.out.println(JacksonUtil.toJSon(addressService.selectAllAddress(phone)));
        return addressService.selectAllAddress(phone);
    }
    /**
     * 查询详细地址
     *
     * @param id
     * @return
     */
    @GetMapping("/selectAddress/{addressId}")
    @ApiOperation(value = "获取地址详细信息",httpMethod = "GET")
    public Result selectAddressById(@PathVariable(value = "addressId") Long id){
        return addressService.selectAddressById(id);
    }
}
